package com.renyi.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.renyi.http.HttpClient;
import com.renyi.http.HttpExpressResult;
import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.http.Response;
import com.renyi.model.MybatisService;
import com.renyi.model.MybatisServiceDoctor;
import com.renyi.util.CalendarUtil;
import com.renyi.util.ConfigUtil;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class AccountController extends BaseController {

	@Autowired
	private MybatisService mybatisService;

	private Logger log = LogManager.getLogger(this.getClass().getName());

	// 获取账户余额
	@RequestMapping("/getAccount")
	public void getAccount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			// 1-PatientUser患者用户 2-DoctorUser 医生用户
			String userType = map.get("userType") != null ? map.get("userType").toString() : "1";
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("userType", userType);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getAccountSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 充值账户
	@RequestMapping("/recharge")
	public void recharge(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			// 1-PatientUser患者用户 2-DoctorUser 医生用户
			String userType = map.get("userType") != null ? map.get("userType").toString() : "1";
			String orderID = map.get("orderID") != null ? map.get("orderID").toString() : "";
			float money = map.get("money") != null ? Float.parseFloat(map.get("money").toString()) : 0;
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("userType", userType);
			pMap.put("money", money);
			pMap.put("orderID", orderID);
			pMap.put("type", 1);// 1、充值 2、咨询支出 3、药品支出 4、体检支出
			pMap.put("memo", "充值");

			long isExist = this.mybatisService.findIbatisListCount("getAccountIDSQL", pMap);
			int ukey = 0;
			if (isExist > 0) {
				ukey = (Integer) this.mybatisService.updateIbatisObject("updateAccountSQL", pMap);

			} else {
				ukey = (Integer) this.mybatisService.insertIbatisObject("insertAccountSQL", pMap);
			}
			if (ukey > 0) {
				int ikey = (Integer) this.mybatisService.insertIbatisObject("insertAccountBusinessSQL", pMap);
			}
			result.setCode(0); // 0：成功
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 账户扣款
	@RequestMapping("/costAccount")
	public void costAccount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			// 1-PatientUser患者用户 2-DoctorUser 医生用户
			String userType = map.get("userType") != null ? map.get("userType").toString() : "1";
			String orderID = map.get("orderID") != null ? map.get("orderID").toString() : "";
			String type = map.get("type") != null ? map.get("type").toString() : "2";
			String memo = map.get("memo") != null ? map.get("memo").toString() : "";
			float money = map.get("money") != null ? Float.parseFloat(map.get("money").toString()) : 0;
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("userType", userType);
			pMap.put("money", money);
			pMap.put("orderID", orderID);
			pMap.put("type", type);// 1、充值 2、咨询支出 3、药品支出 4、体检支出
			pMap.put("memo", memo);
			long isExist = this.mybatisService.findIbatisListCount("getAccountIDSQL", pMap);
			int ukey = 0;
			if (isExist > 0) {
				ukey = (Integer) this.mybatisService.updateIbatisObject("updateAccountSQL", pMap);
				if (ukey > 0) {
					int ikey = (Integer) this.mybatisService.insertIbatisObject("insertAccountBusinessSQL", pMap);
				}
			} else {
				result.setCode(1); // 1：帐户不存在
			}

			result.setCode(0); // 0：成功
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 获取病人用户信息
	@RequestMapping("/getUserPatient")
	public void getUserPatient(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			String patientID = map.get("patientID") != null ? map.get("patientID").toString() : "";
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("patientID", patientID);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getUserPatientSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 获取病人用户信息
	@RequestMapping("/getUserPatientByChatQQ")
	public void getUserPatientByChatQQ(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			String patientID = map.get("chatQQ") != null ? map.get("chatQQ").toString() : "";
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("chatQQ", patientID);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getUserPatientByChatQQ", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 更新病人用户信息
	@RequestMapping("/updateUserPatient")
	public void updateUserPatient(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String userName = map.get("userName") != null ? map.get("userName").toString() : "";
			String age = map.get("age") != null ? map.get("age").toString() : "0";
			String idCard = map.get("idCard") != null ? map.get("idCard").toString() : "";
			// 性别 0-女 1-男
			String sex = map.get("sex") != null ? map.get("sex").toString() : "1";
			String jwbs = map.get("jwbs") != null ? map.get("jwbs").toString() : "";
			String ywgms = map.get("ywgms") != null ? map.get("ywgms").toString() : "";
			String address = map.get("address") != null ? map.get("address").toString() : "";
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("userName", userName);
			pMap.put("age", age);
			pMap.put("idCard", idCard);
			pMap.put("sex", sex);
			pMap.put("jwbs", jwbs);
			pMap.put("ywgms", ywgms);
			pMap.put("address", address);
			int ukey = (Integer) this.mybatisService.updateIbatisObject("updateUserPatientSQL", pMap);
			if (ukey > 0) {
				ukey = (Integer) this.mybatisService.updateIbatisObject("updateMSUserPatientSQL", pMap);
				result.setCode(0); // 0：成功
			} else {
				result.setCode(-1); // 0：成功
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 更新咨询状态 -变为已付款
	@RequestMapping("/updateRecordAccount")
	public void updateRecordAccount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = map.get("orderID") != null ? map.get("orderID").toString() : "";
			float money = map.get("money") != null ? Float.parseFloat(map.get("money").toString()) : 0;
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			pMap.put("money", money);
			pMap.put("out_trade_no", orderID);
			List<HashMap<String, Object>> listRes = this.mybatisService.procedureMybatisList("updateRecordAccountSQL",
					pMap);
			String resType = "";
			if (listRes.size() > 0) {
				resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
				if (resType.equals("0")) {
					List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getZRecordSQL", pMap);
					if (data.size() > 0) {
						String pTel = data.get(0).get("PatientQQ").toString();
						String dTel = data.get(0).get("DoctorQQ").toString();
						if (!"".equals(pTel) && !"".equals(dTel)) {
							pTel = pTel.substring(0, pTel.length() - 1);
							dTel = dTel.substring(0, dTel.length() - 1);
							this.sendPaySMS(dTel, "亲，手机号" + pTel + "的用户向您发起在线咨询，请及时关注", "短信通知");
						}

					}
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("失败");
				}

			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 更新药品状态 -变为已付款
	@RequestMapping("/updateMedicineOrderAccount")
	public void updateMedicineOrderAccount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = map.get("orderID") != null ? map.get("orderID").toString() : "";
			float money = map.get("money") != null ? Float.parseFloat(map.get("money").toString()) : 0;
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			pMap.put("money", money);
			List<HashMap<String, Object>> listRes = this.mybatisService
					.procedureMybatisList("updateMedicineOrderAccountSQL", pMap);
			String resType = "";
			if (listRes.size() > 0) {
				resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
				if (resType.equals("0")) {
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("失败");
				}

			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 更新康美药品状态 -变为已付款
	@RequestMapping("/updateMedicineOrderKMAccount")
	public void updateMedicineOrderKMAccount(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = map.get("orderID") != null ? map.get("orderID").toString() : "";
			float money = map.get("money") != null ? Float.parseFloat(map.get("money").toString()) : 0;
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			pMap.put("money", money);
			List<HashMap<String, Object>> listRes = this.mybatisService
					.procedureMybatisList("updateMedicineOrderKMAccountSQL", pMap);
			String resType = "";
			if (listRes.size() > 0) {
				resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
				if (resType.equals("0")) {
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("失败");
				}

			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 更新门诊订单状态 -变为已付款
	@RequestMapping("/updateMSAccount")
	public void updateMSAccount(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = map.get("orderID") != null ? map.get("orderID").toString() : "";
			float money = map.get("money") != null ? Float.parseFloat(map.get("money").toString()) : 0;
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			pMap.put("money", money);
			List<HashMap<String, Object>> listRes = this.mybatisService.procedureMybatisList("updateMSAccountSQL",
					pMap);
			String resType = "";
			if (listRes.size() > 0) {
				resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
				if (resType.equals("0")) {
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("失败");
				}

			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}
}
